package com.example.testpulsar.mq.factory;

import com.alibaba.fastjson.JSONObject;
import com.example.testpulsar.mq.sender.MqMessageSender;
import org.apache.pulsar.client.api.Producer;

import java.io.Serializable;

/**
 * @description: pulsar消息发送工具类
 * @author LiuZihan
 * @date 2021/8/16 11:05
 * @version 1.0
 */
public class PulsarMessageSender implements MqMessageSender {
    private Producer<String> producer;

    @Override
    public void send(Serializable message) {
        producer.sendAsync(JSONObject.toJSONString(message)).thenAccept(msgId -> {
            System.out.println("Message with ID " + msgId + " successfully sent");
            //TODO 日志处理，记录发送每条数据
        });
    }

    public PulsarMessageSender() {
    }

    public PulsarMessageSender(Producer<String> producer) {
        this.producer = producer;
    }

    public Producer<String> getProducer() {
        return producer;
    }

    public void setProducer(Producer<String> producer) {
        this.producer = producer;
    }
}
